<?php

namespace app\common\model;

use app\common\exception\BusinessException;

class UserVip extends Base
{
    protected $createTime = 'c_time';
    protected $updateTime = 'u_time';


    /**
     * 检查UID是否VIP
     * @param $uid
     * @return bool
     * @throws
     */
    public function checkIsVip($uid): bool
    {
        // 查询条件
        $time = time();
        $where = [
            ['uid', '=', $uid],
            ['expire', '>', $time],
        ];

        // 查询VIP信息
        $count = $this->where($where)->count();
        return (bool)$count;
    }

    /**
     * 检查UID是否课程VIP
     * @param $uid
     * @param $course_id
     * @return bool
     * @throws
     */
    public function checkIsCourseVip($uid, $course_id): bool
    {
        // 查询条件
        $time = time();
        $where = [
            ['uid', '=', $uid],
            ['expire', '>', $time],
        ];

        // 查询全部VIP信息
        $cache = 86400 * 31;
        $list = $this->where($where)->field('profession_id')->select();
        $list = $list ? $list->toArray() : [];
        if (!$list) {
            return false;
        }

        // 查询是否包含课程
        $all_profession_id = array_column($list, 'profession_id');
        $where = [
            ['profession_id', 'in', $all_profession_id],
            ['course_id', '=', $course_id],
        ];
        $cache = 3600;
        return (bool)app(ProfessionCourse::class)->cache($cache)->where($where)->count();
    }

    /**
     * 查询会员信息
     * @param $uid
     * @param $profession_id
     * @return mixed
     * @throws
     */
    public function getInfo($uid, $profession_id)
    {
        // 查询条件
        $time = time();
        $where = [
            ['uid', '=', $uid],
            ['profession_id', '=', $profession_id],
            ['expire', '>', $time],
        ];

        // 查询VIP信息
        return $this->where($where)->find();
    }

    /**
     * 设置会员有效期
     * @param $uid
     * @param $profession_id
     * @param $expire
     * @param $expire_type
     * @throws
     */
    public function setExpire($uid, $profession_id, $expire, $expire_type)
    {
        // 查询条件
        $where = [
            'uid' => $uid,
            'profession_id' => $profession_id,
        ];

        // 查询VIP信息
        $vipInfo = $this->where($where)->find();

        // 根据VIP存在是否设置有效期
        $time = time();
        if ($vipInfo) {
            $now_expire = $vipInfo['expire'] >= $time ? $vipInfo['expire'] : $time;
            $saveData = [
                'expire' => app(UserVipCombo::class)->getTimeByExpireType($expire, $expire_type, $now_expire),
            ];
            $isUpdate = $this->where($where)->save($saveData);
            if ($isUpdate === false) {
                throw new BusinessException('更新用户VIP表失败');
            }
        } else {
            $saveData = [
                'uid' => $uid,
                'profession_id' => $profession_id,
                'expire' => app(UserVipCombo::class)->getTimeByExpireType($expire, $expire_type, $time),
                'a_time' => $time,
                'u_time' => $time,
            ];
            $isUpdate = $this->save($saveData);
            if ($isUpdate === false) {
                throw new BusinessException('新增用户VIP表失败');
            }
        }
    }

    /**
     * deleteById
     * @param $vid
     * @return void
     * @throws BusinessException
     */
    public function deleteById($vid)
    {
        $where = ['id' => $vid];
        $isDelete = $this->where($where)->delete();
        if (!$isDelete) {
            throw new BusinessException('清空会员信息失败');
        }
    }

    /**
     * getVipList
     * @param $uid
     * @param bool $checkEfficient
     * @return array
     */
    public function getVipList($uid, bool $checkEfficient = false): array
    {
        // 查询条件
        $where = [
            ['uid', '=', $uid],
        ];
        if ($checkEfficient) {
            $time = time();
            $where[] = ['expire', '>', $time];
        }

        // 查询数据
        $field = 'a.id as vid,a.expire,b.name,b.old_name,b.layer';
        $list = $this->where($where)
            ->alias('a')
            ->field($field)
            ->leftJoin('profession b', 'a.profession_id = b.id')
            ->order('a.id desc')
            ->select();

        // 返回
        return $list ? $list->toArray() : [];
    }
}